var PATH_PREFIX ="";
var TODO_PATH = "";
var tableIndexs = "1";

function setPathPrefix(path){
	PATH_PREFIX =  path;
}
function setPathPreTodo(path){
	TODO_PATH = path;
}
function initPage(reportTimeBeg,reportTimeEnd,reportType,fileUpload){
	if(reportTimeBeg != null && reportTimeBeg != ''){
		$("#q-reportTime").val(reportTimeBeg + " - " + reportTimeEnd);
	}
	if(reportType != null && reportType != ''){
		$("#q-reportType").val(reportType);
	}
	if(fileUpload != null && fileUpload != ''){
		if(fileUpload == 'E'){
			$(".todoStatus").each(function(){
				if($(this).attr("value") == 'E'){
					$(this).removeClass('fa-square');
		    		$(this).addClass('fa-check-square');
				}
			});
		}else if(fileUpload == 'NE'){
			$(".todoStatus").each(function(){
				if($(this).attr("value") != 'E'){
					$(this).removeClass('fa-square');
		    		$(this).addClass('fa-check-square');
				}
			});
		}
		
	}
	
	if(reportType > 4){
		tableIndexs = "2";
		$('#investNotice2').addClass('active');
		$('#investNotice1').removeClass('active');
		$('#investNoticeDiv').hide();
		$('#investRegularNoticeDiv').show();
		
		$("#q-reportTypeNotice").val(reportType);
	}
}
$(function() {
	// 给select2组件加载样式
	WASP_WIDGET.initializeSelectStyle();
	//项目经理
	WASP_WIDGET.triggerSelectOnEmpId("#q-mgrId");
	//部门
	WASP_WIDGET.triggerSelectOnDeptId("q-deptId");
	//初始化日期
	WASP_WIDGET.triggerDateRangeStyle("q-reportTime");
	//初始化日期
	WASP_WIDGET.triggerDateRangeStyle("q-dataUploadDate");
	// 注册清空事件
    WASP_WIDGET.registerResetClearEvent();
    //选中样式
    chosenStyle();
    //待办状态选中
    INVEST_REPORT.initChoose("todoStatus");
});
//连接跳转过来也要选中样式
function chosenStyle(){
	//var tzzbg = window.parent.document.getElementById("tzzbg");
//	var location = $(tzzbg).data("location");
	//window.parent.pageApp.changeLocation($(tzzbg));
//    $(tzzbg).addClass('active');
//    var gzhszl = window.parent.document.getElementById('gzhszl');
//    $(gzhszl).removeClass('active');
}
var $investReportList = $('#investReportList');

var INVEST_REPORT = {
	investReportDetail : function(){
		var url = PATH_PREFIX + "goPrdInfoPage.do?method=detail";
		openDialogNew(url);
	},
	investUpload : function(investRepId,investUploadType){
		//var url = PATH_PREFIX + "investUpload.do?investRepId="+investRepId+"&investUploadType="+investUploadType;
		var url = TODO_PATH + "toEndFileUploadView4Biz.do?bizId="+investRepId+"&bizType=INFO_DIS_INVESTER";
		window.open(url);
	},
	batchDownAttach : function(investUploadType){
		var ids = $investReportList.jqGrid('getGridParam', 'selarrrow');//选择多选
		if(ids.length == 0){
			ctools.alert('请至少选择一条数据!',"","warning");
			return;
		}
		var investRepIds = ids.join(",");
		INVEST_REPORT.down(investRepIds,investUploadType,'BATCH');
	},
	insertDownload : function(investRepIds,investUploadType){
		INVEST_REPORT.down(investRepIds,investUploadType,'SINGLE');
	},
	down : function(investRepIds,investUploadType,downType){
		var actionUrl = "";
		var fileType = "";
		var message = "";
		if(investUploadType == 'INVEST_DATA'){
			actionUrl = encodeURI(TODO_PATH+"/batchTodoFileDownload4Biz.do?bizType=INFO_DIS_INVESTER&bizId=" + investRepIds);
			fileType = "input";
			message = "该数据未上传材料!";
		}else if(investUploadType == 'INVEST_FILE'){
			actionUrl = encodeURI(TODO_PATH+"/batchEndFileDownload4Biz.do?bizType=INFO_DIS_INVESTER&bizId=" + investRepIds);
			fileType = "output";
			message = "该数据未上传报告!";
		}
		
		$.ajax({
			type:"GET",
			url: TODO_PATH + "/validateExistsFile.do",
			data:{"bizType" : "INFO_DIS_INVESTER",
				"bizId" : investRepIds,
				"fileType" : fileType},
			cache:false, 
			dataType:"json",
			success:function(data){
				if(data.success){
				    window.location = actionUrl;
				}else {
					if(investUploadType == "INVEST_DATA"){
						ctools.alert(message,"","warning");
					}else if(investUploadType == "INVEST_FILE"){
						ctools.alert(message,"","warning");
					}
				}
			}
		});
	},
	changeTable : function(tableIndex){
		tableIndexs = tableIndex;
		if(tableIndex == '1'){
			$('#investNoticeDiv').show();
			$('#investRegularNoticeDiv').hide();
			queryByCondtion(true);
		}else if(tableIndex == '2'){
			$('#investNoticeDiv').hide();
			$('#investRegularNoticeDiv').show();
			queryByCondtion(true);
		}
	},
	//是否选中(样式切换)
	initChoose : function(att){
	    $('.'+att).on('click', function () {
	    	var attr = $(this).attr("class");
	    	if(attr.indexOf("fa-square") > -1){
	    		$(this).removeClass('fa-square');
	    		$(this).addClass('fa-check-square');
			}else{
				$(this).removeClass('fa-check-square');
	    		$(this).addClass('fa-square');
			}
		});
	}
};

$(function() {
	var reportTimeBeg = $("#q-reportTime").getDateRangeValue()[0];
	var reportTimeEnd = $("#q-reportTime").getDateRangeValue()[1];
	var todoStatus = "";
	$(".todoStatus").each(function(){
		if($(this).attr("class").indexOf("fa-check-square") > -1){
			todoStatus += $(this).attr("value") + ",";
		}
	});
	
	$investReportList.jqGrid({   
		url:PATH_PREFIX+'investReportListPage.do',
		/*caption:'招商财富产品列表',  */
		datatype: "json",
		colNames:["投资者报告ID","产品名称","产品代码","报告日期","报告类型ID","报告类型",
		          "状态ID","状态","公告材料上传日期","","操作"],
		colModel:[  { name: 'investRepId', index: 'investRepId', key: true, hidden : true },       
		            { name: 'prjsname', index: 'prjsname', width: 30, resizable: true, sortable: false },
		            { name: 'outerId', index: 'outerId', width: 20, resizable: true, sortable: false },
		            { name: 'reportTime', index: 'reportTime', width: 30, resizable: true, sortable: false },
		            { name: 'reportType', index: 'reportType', hidden : true },
		            { name: 'reportName', index: 'reportName', width: 25, resizable: true, sortable: false },
		            { name: 'todoStatus', index: 'todoStatus', hidden : true },
		            { name: 'todoStatusNm', index: 'todoStatusNm', width: 30, resizable: true, sortable: false },
		            { name: 'dataUploadDate', index: 'dataUploadDate', width: 30, resizable: true, sortable: false },
		            { name: 'noFile', index: 'noFile', hidden : true },
		            { name: 'option', index: 'option', width: 80, resizable: true, sortable: false }
				  ],
		rowNum: 20,
        rowList: [20, 30, 50],
        rownumbers: true,
        rownumWidth: 50,
        prmNames: { search: "search", page: "pageNo", rows: "limit" },
        height: 'auto',
        width: false,
        autowidth:true,
        editurl: '',
        viewrecords: true,
        postData:{'sp[tableIndexs]' : tableIndexs,
        	'sp[reportTimeBeg]':reportTimeBeg,
            'sp[reportTimeEnd]':reportTimeEnd,
            'sp[reportType]':reportType,
            'sp[todoStatus]':todoStatus},
        cellEdit: false,
        shrinkToFit: true,
        grouping: false,
        jsonReader: {
            root: "items", //结果集
            records: "total", //总记录数 
            total: "pageCount", //总页数
            page: "pageNo", //当前页 
            repeatitems: false // (4) 
        },  
		multiselect:true,
		pager: "#investReportListPage",					
		viewrecords: true,
		hidegrid: false, 
		gridComplete: function(){
			var ids = $investReportList.jqGrid('getDataIDs');
			for(var i=0;i < ids.length;i++){
				var rowId = ids[i];
				var rowData = $investReportList.jqGrid('getRowData', rowId); 
				var todoStatus = rowData.todoStatus;
				var noFile = rowData.noFile;
				var params = {
						'dataDown'		   : 'insertDataDownloadTemp',
						'up' 			   : 'insertListUploadTemp',
						'down'             : 'insertListDownloadTemp'
					};
				var permissionButton = Permission_Button.configPermissionButton(params);
				if(todoStatus == 'C'){
					permissionButton.up = Permission_Button.clickPermissionButton('insertListUploadTemp',"INVEST_REPORT.investUpload('" + rowId + "','INVEST_FILE');");
				}
				//材料已上传、材料已确认、已完成
				if(todoStatus == 'E'){
					permissionButton.down = Permission_Button.clickPermissionButton('insertListDownloadTemp',"INVEST_REPORT.insertDownload('" + rowId + "','INVEST_FILE');");
				}
				//permissionButton.dataUp = Permission_Button.clickPermissionButton('insertDataUploadTemp',"INVEST_REPORT.investUpload('" + rowId + "','INVEST_DATA');");
				//已完成
				if(noFile == 'N' && (todoStatus == 'S' || todoStatus == 'C' || todoStatus == 'E')){
					permissionButton.dataDown = Permission_Button.clickPermissionButton('insertDataDownloadTemp',"INVEST_REPORT.insertDownload('" + rowId + "','INVEST_DATA');");
				}
				$investReportList.jqGrid('setRowData', ids[i], { option: Permission_Button.linkAllBtn(permissionButton)});
			}	
		}
	});
	$investReportList.navGrid('#investReportListPage',{edit:false,add:false,del:false,search:false,refreshstate:'current'});
	$investReportList.jqGrid('setFrozenColumns');
	jqGridResize($investReportList);
	queryByCondtion(false);
});

function queryByCondtion(flag) {
	var postData = $investReportList.jqGrid("getGridParam", "postData");
	var prjsname = $("#q-prjsname").val().trim();
	var outerId = $("#q-outerId").val().trim();
	var reportTimeBeg = $("#q-reportTime").getDateRangeValue()[0];
	var reportTimeEnd = $("#q-reportTime").getDateRangeValue()[1];
	var todoStatus = "";
	var dataUploadBeg = $("#q-dataUploadDate").getDateRangeValue()[0];
	var dataUploadEnd = $("#q-dataUploadDate").getDateRangeValue()[1];
	
	$(".todoStatus").each(function(){
		if($(this).attr("class").indexOf("fa-check-square") > -1){
			todoStatus += $(this).attr("value") + ",";
		}
	});
	
	var reportType = "";
	if(tableIndexs == '1'){
		reportType = $("#q-reportType").val();
	}else if(tableIndexs == '2'){
		reportType = $("#q-reportTypeNotice").val();
	}
    $.extend(postData, {
        'sp[prjsname]':prjsname,
        'sp[outerId]':outerId,
        'sp[reportTimeBeg]':reportTimeBeg,
        'sp[reportTimeEnd]':reportTimeEnd,
        'sp[reportType]':reportType,
        'sp[tableIndexs]':tableIndexs,
        'sp[todoStatus]' : todoStatus,
        'sp[dataUploadBeg]' : dataUploadBeg,
        'sp[dataUploadEnd]' : dataUploadEnd
    });
    if (flag) {
    	$investReportList.trigger("reloadGrid", [{ page: 1 }]); //重新载入Grid表格
    } else {
    	$investReportList.trigger("reloadGrid"); //重新载入Grid表格
    }
};

function clears(){
	$(".todoStatus").each(function(){
		var attr = $(this).attr("class");
    	if(attr.indexOf("fa-check-square") > -1){
    		$(this).removeClass('fa-check-square');
    		$(this).addClass('fa-square');
		}
	});
}

//绑定回车事件,查询
$("body").keypress(function(event){
	var key = event.which;
	if(key == "13"){
		queryByCondtion(true);
	}
});